﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace hamster
{
    class Fruit : WorldObject
    {
        public const float FRUIT_SIZE = 0.2f;
        public const int FRUIT_SCORE = 100;

        public Fruit(Vector2 position, float rotation, ObjectPlacer parent) : base(position, rotation, parent)
        {
            objectScale_ = FRUIT_SIZE;
            debugColor_ = Color.Orange;
            localTransformations_ = Matrix.CreateRotationZ(Game1.RandomFloat * 6.23f);
        }
        float offset = Game1.RandomFloat;
        public override void Draw(GraphicsDevice device)
        {//if (Keyboard.GetState().IsKeyDown(Keys.Space))
            //{
            //    color.X = Game1.RandomFloat ;
            //    color.Y = Game1.RandomFloat;
            //    color.Z = Game1.RandomFloat;
            //    color.W = 0;
            //    color *= 1.15f;
            //}
 
            ObjectPlacer._objectEffect.Parameters["World"].SetValue(World);
            ObjectPlacer._objectEffect.Parameters["Time"].SetValue(offset + Game1.time_duration);
            

            ObjectPlacer._objectEffect.CommitChanges();
            device.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, ObjectPlacer._quad, 0, ObjectPlacer._quad.Length / 3);

            /*
            Vector3 upDir = Vector3.Transform(Vector3.Up, Matrix.CreateRotationZ(rotation_));
            Game1.DrawDebugSquare(localTransformations_ * Matrix.CreateScale(objectScale_) * Matrix.CreateWorld(new Vector3(position_, -1), -Vector3.UnitZ, upDir), debugColor_); 
            */
            //Game1.spriteBatch.dr

            //base.Draw(device);
        }

        public override bool Update(float dt, Player player)
        {
            base.Update(dt, player);
            // If the player hits the fruit, eat it
            if (boundingBox_.Intersects(player.boundingBox))
            {
                player.score += FRUIT_SCORE;
                // TODO Play a eating sound.
                // Add waste
                WasteGoomba waste = new WasteGoomba(new Vector2(player.position.X, player.position.Y), player.rotation, parent_);
                parent_.activeWaste_.Add(waste);

                for (int i = 0; i < 50; i++)
                    Particles.instance.Add(player.position, (new Vector3( player.velocity , 0) + Game1.RandomVector) * 2f, Game1.RandomFloat + 0.15f, Game1.RandomFloat + .05f, 1f);

                return true;
            }

            // Don't want to remove plant
            return false;
        }
    }
}
